package menu.lt.pers.menu.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import menu.lt.pers.menu.R;
import menu.lt.pers.menu.ResultByName;

import java.util.List;

public class SearchRecyclerViewAdapter extends RecyclerView.Adapter<SearchRecyclerViewAdapter.MyHolder>{
    private Context context;
    private List<String> list;

    public SearchRecyclerViewAdapter(Context context,
                                     List<String> list) {
        this.context = context;
        this.list = list;
    }
    public Context getContext() {
        return context;
    }
    public void update(List<String> list){
        this.list = list;
        notifyDataSetChanged();
    }

    public List<String> getList() {
        return list;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater
                .from(viewGroup.getContext())
                .inflate(R.layout.history_resource,viewGroup,false);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
        myHolder.tv_search_content.setText(list.get(i));
        final int index = i;
        myHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ResultByName.class);
                intent.putExtra("search_text",list.get(index));
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        TextView tv_search_content;
        public MyHolder(@NonNull View itemView) {
            super(itemView);
            tv_search_content = itemView.findViewById(R.id.record);
        }
    }

}
